<?php


namespace app\model;


use app\services\AliPayServices;
use app\services\BankServices;
use app\services\SendCode;
use think\facade\Db;
use think\Model;

class WithdrawalLog extends Model
{

    /**
     * 提现
     * @param $data
     * @return mixed
     */
    public function withdrawal($data)
    {
        RedisLock('Withdrawal_' . $data->user_id);
        security_password_verify($data); // 验证安全密码
        app()->make(SendCode::class)->CodeVerify($data->data['code'], $data->user->phone, 2);
        try {
            switch ($data->data['channel']) {
                case 'alipay'; // 支付宝提现
                    return AliPayServices::instance()->userId($data->user_id)->data($data->data)->withdrawal();
                    break;
                case 'bank';//银行卡提现
                    return BankServices::instance()->uid($data->user_id)->data($data->data)->withdrawal();
                    break;
            }
        } catch (\Exception $e) {
            return app('json')->json_error('提现失败1' . $e->getMessage());
        }
    }


    /**
     * 记录
     * @param $param
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function WithdrawalLog($param)
    {
        $result = Db::name('withdrawal_log')->where('user_id', $param->user_id)
            ->field('apply_money as practical_money,create_at,status,fail_log')
            ->order('create_at', 'desc')
            ->paginate($param->data['limit'])
            ->toArray();
        return app('json')->json_success('成功', $result);
    }
}